package cn.com.haopy.yue.ai.core.system;

import cn.com.haopy.yue.ai.core.utils.SystemInfoPrinter;
import cn.com.haopy.yue.ai.db.service.hos.HOSSystemService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 系统启动服务，用于设置系统配置信息、检查系统状态及打印系统信息
 */
@Component
@RequiredArgsConstructor
class SystemInistService {
    private SystemInistService systemInistService;

    private final Environment environment;

    private final HOSSystemService systemService;

    @PostConstruct
    private void inist() {
        systemInistService = this;
        initConfigs();
        SystemInfoPrinter.printInfo("Haopy ARisk  初始化信息", getSystemInfo());
    }

    private final static Map<String, String> DEFAULT_CONFIGS = new HashMap<>();

    static {
        DEFAULT_CONFIGS.put(SystemConfig.SYSTEM_RISK_HUMAN_SOURCE_COUNT, "1");
        DEFAULT_CONFIGS.put(SystemConfig.SYSTEM_RISK_LEVEL_LIST, "[\"极低风险\",\"低风险\",\"中低风险\",\"中风险\",\"中高风险\",\"高风险\",\"极高风险\"]");
        DEFAULT_CONFIGS.put(SystemConfig.SYSTEM_UPLOAD_FILE_FORMAT, "jpg,jpeg,png,pdf");
        DEFAULT_CONFIGS.put(SystemConfig.SYSTEM_SUMMARY_RESULT_CONTENT, "");
        DEFAULT_CONFIGS.put(SystemConfig.USER_RISK_WX_PROTOCOL, "");
        DEFAULT_CONFIGS.put(SystemConfig.USER_RISK_WX_PRIVACY, "");
        DEFAULT_CONFIGS.put(SystemConfig.VIP_CHARGE_MONEY, "10.00");
        DEFAULT_CONFIGS.put(SystemConfig.VIP_CHARGE_MONTH, "12");
    }

    private void initConfigs() {
        // 1. 读取数据库全部配置信息
        Map<String, String> configs = systemService.queryAll();

        // 2. 分析DEFAULT_CONFIGS
        for (Map.Entry<String, String> entry : DEFAULT_CONFIGS.entrySet()) {
            if (configs.containsKey(entry.getKey())) {
                continue;
            }

            configs.put(entry.getKey(), entry.getValue());
            systemService.addConfig(entry.getKey(), entry.getValue());
        }

        SystemConfig.setConfigs(configs);
    }

    private Map<String, String> getSystemInfo() {

        Map<String, String> infos = new LinkedHashMap<>();

        infos.put(SystemInfoPrinter.CREATE_PART_COPPER + 0, "系统信息");
        // 测试获取application-db.yml配置信息
        infos.put("服务器端口", environment.getProperty("server.port"));
        infos.put("数据库USER", environment.getProperty("spring.datasource.druid.username"));
        infos.put("数据库地址", environment.getProperty("spring.datasource.druid.url"));
        infos.put("调试级别", environment.getProperty("logging.level.cn.com.haopy.nethospital"));


        infos.put("对象存储", environment.getProperty("haopyYueAi.storage.active"));
        infos.put("本地对象存储路径", environment.getProperty("haopyYueAi.storage.local.storagePath"));
        infos.put("本地对象访问地址", environment.getProperty("haopyYueAi.storage.local.address"));
        infos.put("本地对象访问端口", environment.getProperty("haopyYueAi.storage.local.port"));

        infos.put("数字人资源数量", SystemConfig.getSystemRiskHumanSourceCount() + "");


        return infos;
    }
}
